#pragma once

#include <QObject>
#include <QSqlDatabase>
#include "DatabaseThread.h"
#include <qvariant.h>

namespace database
{
	class UseMySQL : public QObject
	{
		Q_OBJECT

	private:
		friend class DatabaseThread;

		UseMySQL(QObject* parent = nullptr);

		int countPlaceholders(const QString& queryStr);
		bool mainExecuteTask(std::shared_ptr<const DatabaseTask>& task, std::shared_ptr<DatabaseTaskResult>& taskResult);
		bool executeSelectTask(std::shared_ptr<const DatabaseTask>& task, std::shared_ptr<DatabaseTaskResult>& taskResult);
		bool executeOtherTask(std::shared_ptr<const DatabaseTask>& task, std::shared_ptr<DatabaseTaskResult>& taskResult);

	private:
		bool connectMySQL();
		bool init();
		bool transaction();
		bool commit();
		bool rollback();

	public:
		~UseMySQL();

	private:
		QSqlDatabase _database;
	};
}